import Device from '@skillnull/device-js'
import Cookies from 'js-cookie'
import request from '..'
import { host, port } from '@/constants'
let uniqueVisitor = {}
Device.Info({
  info: [
    'deviceType',
    'OS',
    'OSVersion',
    'OSVersion',
    'screenWidth',
    'screenHeight',
    'browserInfo',
    'UUID',
  ],
}).then(data => {
  uniqueVisitor = data
})

// uv
const sendUserPointRequest = data => {
  if (data.length === 0) return
  if (Cookies.get('UUID')) {
    uniqueVisitor = null
  } else {
    Cookies.set('UUID', uniqueVisitor.UUID, { expires: 1 })
  }
  navigator.sendBeacon(
    `${host}:${port}/user/log`,
    JSON.stringify({ data, uniqueVisitor })
  )
}

// pv
const sendPageViewRequest = async pageView => {
  console.log(pageView)
  if (pageView.length === 0) return
  await request({
    method: 'post',
    url: '/user/pv',
    data: {
      pageView,
    },
  })
}

export { sendUserPointRequest, sendPageViewRequest }
